package com.example.p2pinvest.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.p2pinvest.R;
import com.example.p2pinvest.common.BaseActivity;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LineChartActivity extends BaseActivity implements OnChartValueSelectedListener {


    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_title_setting)
    ImageView ivTitleSetting;
    @BindView(R.id.line_chart)
    LineChart lineChart;

    private String[] dates;

    @Override
    protected void initData() {

        dates = new String[Calendar.DST_OFFSET];
        for (int i = 1; i <= Calendar.DST_OFFSET; i++) {
            dates[i - 1] = i + "";
        }

        lineChart.setOnChartValueSelectedListener(this);

        lineChart.setDrawGridBackground(true); //是否绘制网格背景
        Description description = new Description();

        description.setText("8月中东降雨量");
        lineChart.setDescription(description);

        lineChart.setDrawBorders(false);

        lineChart.getAxisLeft().setEnabled(false);  //隐藏左轴线
        lineChart.getAxisRight().setDrawAxisLine(false);
        lineChart.getAxisRight().setDrawGridLines(true);
        lineChart.getXAxis().setDrawAxisLine(true);  //是否绘制X轴的轴线
        lineChart.getXAxis().setDrawGridLines(false);  //是否绘制X轴的网格线
        lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);  //设置X轴在底部

        // enable touch gestures
        lineChart.setTouchEnabled(true);

        // enable scaling and dragging
        lineChart.setDragEnabled(true);
        lineChart.setScaleEnabled(true);

        // if disabled, scaling can be done on x- and y-axis separately
        lineChart.setPinchZoom(false);

        Legend l = lineChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);

        lineChart.setData(generateDataLine(1));

        //自定义X轴标签
        lineChart.getXAxis().setValueFormatter((value, axis) -> dates[(int) value]);

        lineChart.animateX(750);  //设置了动画效果则不需要调用invalidate()
        //lineChart.invalidate();
    }

    @Override
    protected void initTitle() {
        tvTitle.setText("折线图demo");
        ivTitleBack.setVisibility(View.VISIBLE);
        ivTitleSetting.setVisibility(View.INVISIBLE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_line_chart;
    }

    @OnClick(R.id.iv_title_back)
    public void back(View view) {
        this.removeCurrentActivity();
    }


    @Override
    public void onValueSelected(Entry e, Highlight h) {
        Log.i("VAL SELECTED",
                "Value: " + e.getY() + ", xIndex: " + e.getX()
                        + ", DataSet index: " + h.getDataSetIndex());
    }

    @Override
    public void onNothingSelected() {

    }

    private final int[] colors = new int[]{
            ColorTemplate.VORDIPLOM_COLORS[0],
            ColorTemplate.VORDIPLOM_COLORS[1],
            ColorTemplate.VORDIPLOM_COLORS[2]
    };

    /**
     * 初始化折线图数据
     *
     * @param cnt
     * @return
     */
    private LineData generateDataLine(int cnt) {
        ArrayList<ILineDataSet> dataSets = new ArrayList<>();

        for (int z = 0; z < cnt; z++) {

            ArrayList<Entry> values = new ArrayList<>();

            for (int i = 0; i <= Calendar.DST_OFFSET; i++) {
                double val = (Math.random() * 65) + 40;
                values.add(new Entry(i, (float) val));
            }

            LineDataSet d = new LineDataSet(values, "毫米");
            d.setLineWidth(2.0f);
            d.setCircleRadius(4f);
            d.setDrawValues(true);

            int color = colors[z % colors.length];
            d.setColor(color);
            d.setCircleColor(color);
            dataSets.add(d);

        }
        return new LineData(dataSets);
    }

}